package com.llxz.blog.article.service.impl;

import com.llxz.blog.article.entity.Label;
import com.llxz.blog.article.service.LabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import org.springframework.data.mongodb.core.query.Query;
import java.util.ArrayList;
import java.util.List;

@Service(value = "LabelService")
@Component
public class LabelServiceImpl implements LabelService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<String> getPopularLabels() {
        Query query = Query.query(Criteria.where("count").gte(1));
        Sort sort = Sort.by(Sort.Direction.DESC, "count");
        query.with(sort);
        List<Label> labels =  mongoTemplate.find(query, Label.class, "label");
        List<String> list = new ArrayList<>();
        for (Label label : labels) {
            list.add(label.getLabel());
        }
        return list;
    }
}